﻿if (typeof Phule == "undefined") Phule = {};
if (typeof Phule.Web == "undefined") Phule.Web = {};
Phule.Web.Main = {
    Init: function () {
        var thisObj = this;
        thisObj.MenuActive();
        thisObj.EventSearchNews();
    },
    timerKeyup:null,
    MenuActive: function (index) {
        $("#navul li").removeClass('active');
        if(typeof activeMenu!="undefined")
            $("#navul li#" + activeMenu).addClass('active');
    },
    Event: function () {
        $(".user-register").click(function () {
            ShowDialogRegister();
        });
        $(".user-login").click(function () {
            ShowDialogLogin();
        });
        $(".main-user-logged-logout").click(function () {
            var overlay = new COverlay({
                autoShow: false
            });
            overlay.Init();
            $.Ajax({
                url: "../Accounts/Service/AccountService.asmx/Logout",
                type: 'POST',
                data: {},
                success: function (data, textStatus, xhr) {
                    if (data) {
                        data = $(data).find("string").text();
                        window.location.href = "../../Views/Home";
                    }
                    overlay.Hide();
                },
                beforeSend: function () {
                    overlay.Show();
                },
                error: function (xhr, textStatus, errorThrown) {
                    overlay.Hide();
                }
            });
        });
        $("#Gift-Slider").find(".jshowoff-item").live("click", function () {
            var projectID = $(this).attr("projectid");
            switch (projectID) {
                case "97":
                case "52":
                case "50":
                    $.CAlert.Alert({
                        Title: "Thông báo",
                        Content: $.string.Format('Dự án đã hết hạn hoặc đã đủ số lượng người tham dự. Mời bạn đến dự án khác.', 1)
                    });
                    return false;
                   
            }


            if (projectID != "0")
                window.location.href = "/Views/Project/Project.aspx?ProjectID=" + projectID;
        })
    },
    DialogAboutUs: {
        Instance: null,
        Init: function () {
            var thisObj = this;
            var _width = 600
            var left = ($(window).width() - _width) / 2;
            thisObj.Instance = new CDialog({
                title: "• Giới thiệu •",
                width: _width,
                zIndex: 999,
                autoOpen: false,
                draggable: false,
                position: [left, 10],
                height: 600,
                open: function () {
                    // if ($("#FormAboutUs").html().length == "") {
                    //                        var url = window.location.href;
                    //                        var arr = url.split("/"); ;
                    //                        var result = arr[0] + "//" + arr[2];

                    $.Ajax({
                        type: "POST",
                        url: '../News/Service/NewsService.asmx/GetNewStatitic',
                        cache: false,
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        data: '{' + $.string.Format('"ID":1', '') + '}',
                        success: function (pData) {
                            if (pData !== null)
                                if (pData.d !== null) {
                                    var data = eval("(" + pData.d + ")");
                                    $("#FormAboutUs").html(data[0].Content);
                                }
                        }
                    });
                    //  }
                },
                beforeClose: function (event, ui) {

                }
            });
            thisObj.Instance.Init("#FormAboutUs");
        },
        Open: function () {
            var thisObj = this;
            if (thisObj.Instance)
                thisObj.Instance.Open();
        },
        Close: function () {
            var thisObj = this;
            if (thisObj.Instance)
                thisObj.Instance.Close();
        }
    },
    EventSearchNews: function () {
        var thisObj = this;
        $('#search-input').bind('keyup', function (evt) {
            clearInterval(thisObj.timerKeyup);  //clear any interval on key up
            thisObj.timerKeyup = setTimeout(function () {
                thisObj.GetSearchData();
            }, 2000);
            return false;
        });
    },
    GetSearchData:function(){
        var htmlNews = '<h1 class="title">Kết quả tìm kiếm</h1>'
			+ '<div class="list">'
				+ '<ul id="news_list_content">'

					+ '<div class="clearfix"></div>'
				+ '</ul>'
			+ '</div>'
			+ '<div class="paging" id="paging">'
				+ '<a href="#">1</a>'
				+ '<a href="#" class="current">2</a>'
				+ '<a href="#">3</a>'
				+ '<a href="#">4</a>'
				+ '<a href="#">5</a>'
			+ '</div>';
        $("#content").html(htmlNews);
        $("#content").addClass("news")
        this.Paging.Init();
    },
    Paging: {
        isFirstPaging: true,
        Init: function () {
            var thisObj = this;
            thisObj.GetData(1, true);
        },
        GetData: function (pPageNumber, bmn) {
            var objThis = this;
            var keyWord = $.trim($("#search-input").val());
            if (keyWord == '') return;
            var rPerPage = 4;
            if (typeof RowPerPage != 'undefined')
                rPerPage = RowPerPage;

            var data = '{' + $.string.Format('"page":{0},"rows":{1},"keyWord":"{2}"', pPageNumber, rPerPage, keyWord) + '}';
            var url = 'Services/NewsService.asmx/GetListNewsSearch';

            $.Ajax({
                type: "POST",
                url: url,
                cache: false,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: data,
                success: function (pData) {
                    var data = eval("(" + pData.d + ")");
                    Phule.Web.Main.ShowNewsList(data);

                    if (data.records == undefined)
                        data.records = rPerPage;
                    // console.log(data, 'data.total', data.total, 'data.records', data.records);
                    if (bmn) {
                        $("#paging").jpaging({
                            all_items_num: data.total,
                            items_on_page: data.records,
                            callback: function (a, b, c) {
                                objThis.GetData(a);
                            }
                        });
                    }

                }
            });
        }
    },
    ShowNewsList: function (data) {
        var htmlContent = "";
        var pattern = "<li>"
						+ '<img src="{0}" />'
						+ '<h2 class="title"><a href="{3}">{1}</a></h2>'
						+ '<p>{2}</p>'
						+ '<a href="{3}" class="readmore">Chi tiết »</a>'
        + "</li>";
        $.each(data.list, function (index, Item) {
            htmlContent += $.string.Format(pattern, Item.ThumbnailImage, Item.Title, Item.Summary, BuildUrlFriendly("newsdetail", Item.ID, Item.Title));
        });
        $("#news_list_content").html(htmlContent);
    }
};
$(function () {
    Phule.Web.Main.Init();
});
function NavigationActive(index) {
    Phule.Web.Main.MenuActive(index);
}

function ShowDialogAboutUs() {
    Axis.Web.Main.DialogAboutUs.Open();
}
function ShowDialogContactUs() {
    Axis.Web.Main.DialogContactUs.Open();
}

